package com.cskaoyan.controller;

import com.cskaoyan.anno.HashPasswordAnno;
import com.cskaoyan.bean.Admin;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.admin.PageAdmins;
import com.cskaoyan.bean.admin.bo.AdminListBo;
import com.cskaoyan.bean.admin.bo.CreateAdminBo;
import com.cskaoyan.bean.admin.bo.DeleteAdminBO;
import com.cskaoyan.bean.admin.bo.UpdateAdminBo;
import com.cskaoyan.bean.admin.vo.AdminListVo;
import com.cskaoyan.bean.admin.vo.CreateAdminVo;
import com.cskaoyan.bean.admin.vo.UpdateAdminVo;
import com.cskaoyan.exeception.BrockenHeartOfMing;
import com.cskaoyan.service.AdminService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @HJL
 */
@RestController
@RequestMapping("admin/admin")
public class AdminAdminController {
    @Autowired
    private AdminService adminService;

    /**
     * 分页查询管理员
     * @param bo
     * @return
     */
    @GetMapping("list")
    public BaseRespVo getAdminList(AdminListBo bo) {
        PageAdmins pageAdmins = adminService.getAdminList(bo);
        AdminListVo volist = pageAdminsToAdminListVo(pageAdmins);
        return BaseRespVo.ok(volist);
    }

    /**
     * 添加管理员
     * @param bo
     * @return
     */
    @HashPasswordAnno
    @PostMapping("create")
    public BaseRespVo createAdmin(@Validated @RequestBody CreateAdminBo bo, BindingResult bindingResult) {
        CreateAdminVo vo = adminService.createAdmin(bo);
        if (vo == null) {
            return BaseRespVo.fail("添加失败");
        }
        return BaseRespVo.ok(vo);
    }

    /**
     * 删除管理员
     * @return
     */
    @PostMapping("delete")
    public BaseRespVo deleteAdmin(@RequestBody DeleteAdminBO bo) {
        int code = adminService.deleteAdmin(bo);
        if (code == 1) {//1 事务失败 0成功
            return BaseRespVo.fail("操作失败");
        }
        return BaseRespVo.ok();
    }

    /**
     * 修改管理员账号信息
     *
     * @param
     * @return
     */
    @HashPasswordAnno
    @PostMapping("update")
    public BaseRespVo updateAdmin(@Validated @RequestBody UpdateAdminBo bo, BindingResult bindingResult) throws SQLException, IllegalAccessException, BrockenHeartOfMing {
        Admin a = new Admin();
        BeanUtils.copyProperties(bo, a);
        Admin principal = (Admin) SecurityUtils.getSubject().getPrincipal();
        Admin admin = adminService.updateAdmin(principal.getUsername(), a);

        UpdateAdminVo vo = new UpdateAdminVo();
        BeanUtils.copyProperties(admin, vo);
        return BaseRespVo.ok(vo);
    }
    //转换 因为service从数据库查询通用的
    private AdminListVo pageAdminsToAdminListVo(PageAdmins pageAdmins) {
        AdminListVo adminListVo = new AdminListVo();
        adminListVo.setTotal(pageAdmins.getTotal());
        List<AdminListVo.ItemsBean> items = new ArrayList<>();
        List<Admin> admins = pageAdmins.getAdmins();
        for (Admin admin : admins) {
            AdminListVo.ItemsBean item = new AdminListVo.ItemsBean();
            item.setId(admin.getId());
            item.setUsername(admin.getUsername());
            item.setAvatar(admin.getAvatar());
            item.setRoleIds(admin.getRoleIds());
            items.add(item);
        }
        adminListVo.setItems(items);
        return adminListVo;
    }



}
